﻿--[[
   @brief    社交推广tips管理
   @author   rickonzhang
   @version  1.0
   @date     2016-06-14
--]]

social_push_tips_mgr = 
{
    marquee_msg_list_ = ierd_tgp.personal_center.MarqueeMsgList:new();

    is_ad_closed_ = false;
    is_im_inited_ = false;

    ad_list_ = {};
    delay_time_ = 0;

    delete_id_list_ = {}; -- { {time=xx, id=xxx}, {}}

    MAX_SAVED_TIME = 60 * 60 * 24 * 20;
};

function social_push_tips_mgr.close_social_ad_tips(event_id, json_str)
    local param = base_utility.json_to_table(json_str);
    social_push_tips_mgr.delete_one_item(param.ad_id);
    social_push_tips_mgr.clear_all_item();
    social_push_tips_view.show_ad_area('false');
    social_push_tips_view.stop_show_timer();
    social_push_tips_view.stop_animation_timer();

    --产品要求关闭广告后，延迟1s再更新其他信息
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:AddInterval('social_push_tips_mgr.process_web_delete_ad', 1000);
end

function social_push_tips_mgr.process_web_delete_ad()
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:RemoveInterval('social_push_tips_mgr.process_web_delete_ad');
    social_title_msg_tips_mgr.update_tips_msg_info();
end

--初始化完毕后读取
function social_push_tips_mgr.on_im_inited(event, json_str)
    -- body
    social_push_tips_mgr.is_im_inited_ = true;
    social_push_tips_mgr.read_cfg();
    social_push_tips_mgr.filter_deleted_id();
    social_push_tips_mgr.write_cfg();
    social_push_tips_view.reset_ad();
    social_title_msg_tips_mgr.update_tips_msg_info({});
end

function social_push_tips_mgr.read_cfg()
    local out = comm_center.call_service('im_app_service_read_common_cfg', {is_self = 1, key = 'social_pop_deleted'});
    local list = base_utility.json_to_table(out["value"]);
    local cur_time = os.time();
    for i = 1, #list do
        --过期删掉，否则保存越来越多
        if cur_time - list[i].time < social_push_tips_mgr.MAX_SAVED_TIME then
            table.insert(social_push_tips_mgr.delete_id_list_, list[i]);
        else
            -- no need
        end
    end
end

function social_push_tips_mgr.write_cfg()
    local save_str = base_utility.table_to_json(social_push_tips_mgr.delete_id_list_);
    comm_center.call_service('im_app_service_write_common_cfg', {is_self = 1, key = 'social_pop_deleted', value = save_str});
end

function social_push_tips_mgr.log(log_str)
    base_utility.log("[social_push_tips_mgr] "..log_str);
end

function social_push_tips_mgr.init()
    social_push_tips_mgr.log("init");
end

function social_push_tips_mgr.on_recv_social_push_tips(event, json_str)
    local personal_center = base_utility.get_component_adapter("IPersonal_center",
                    "ierd_tgp.personal_center.GetPersonalCenterModule();");
    personal_center:get_social_marquee_msg_list(social_push_tips_mgr.marquee_msg_list_);
    social_push_tips_mgr.log(string.format("social tips cnt:%d", social_push_tips_mgr.marquee_msg_list_.dwEntryNum));

    social_push_tips_mgr.delay_time_ = social_push_tips_mgr.marquee_msg_list_.dwInterval;

    if social_push_tips_mgr.is_im_inited_ == true then
        social_push_tips_mgr.filter_deleted_id();
        social_push_tips_view.reset_ad();
        social_title_msg_tips_mgr.update_tips_msg_info({});
    end

end

function social_push_tips_mgr.filter_deleted_id()    
    social_push_tips_mgr.clear_all_item();
    for i = 1, social_push_tips_mgr.marquee_msg_list_.dwEntryNum do
        --过滤
        if social_push_tips_mgr.is_msg_id_in_delete(social_push_tips_mgr.marquee_msg_list_.astEntryList[i-1].ullMsgID) == false then
            local item = {};
            item.id = social_push_tips_mgr.marquee_msg_list_.astEntryList[i-1].ullMsgID;
            item.title = social_push_tips_mgr.marquee_msg_list_.astEntryList[i-1].szContent;
            item.url = "";

            local str = social_push_tips_mgr.marquee_msg_list_.astEntryList[i-1].szLinkUrl;
            local param = base_utility.json_to_table(str);
            if param ~= nil and param.url ~= nil then
                item.url = param.url;
            end
            table.insert(social_push_tips_mgr.ad_list_, item);
        end
    end
end

function social_push_tips_mgr.is_msg_id_in_delete(tips_id)
    for i, id_info in ipairs(social_push_tips_mgr.delete_id_list_) do
        if tips_id == id_info.id then
            return true;
        end
    end
    return false;
end

function social_push_tips_mgr.clear_all_item()
    social_push_tips_mgr.ad_list_ = {};
end

function social_push_tips_mgr.delete_one_item(id)
    if #social_push_tips_mgr.ad_list_ > 0 then
        for index, item in ipairs(social_push_tips_mgr.ad_list_) do
            if item.id == id then
                table.remove(social_push_tips_mgr.ad_list_, index);

                local id_info = {};
                id_info.time = os.time();
                id_info.id = id;
                table.insert(social_push_tips_mgr.delete_id_list_, id_info);
                social_push_tips_mgr.write_cfg();
                break;
            end
        end
    end
end

function social_push_tips_mgr.get_ad_list()
    --社交初始化完之前不返回
    local tips_list = nil;
    if social_push_tips_mgr.is_im_inited_ == true then
        if #social_push_tips_mgr.ad_list_ > 0 then
            tips_list = {};
            tips_list.delay = social_push_tips_mgr.delay_time_ * 1000;
            tips_list.list = social_push_tips_mgr.ad_list_;
        end
    end
    return tips_list;
end

function social_push_tips_mgr.click_session_tab_btn(event_id, json_str)
    social_push_tips_mgr.try_report_show();
end

function social_push_tips_mgr.click_contact_tab_btn(event_id, json_str)
    social_push_tips_mgr.try_report_show();
end

function social_push_tips_mgr.try_report_show()
    local data = comm_center.call_service('i_service_social_mainwnd_is_shown', {});
    if data.is_show == 0 then --min mode
        if social_push_tips_view.is_ad_show() == "true" then
            if social_push_tips_view.is_report == nil then
                social_push_tips_view.is_report = true;
                comm_center.call_service('i_service_increase_mini_mode_social_ad_show_cnt', {});
            end
        end
    end
end
--------------------------------------ui control -------------------------------------------------
social_push_tips_view = 
{
    max_tips_root_id_1_ = 0;
    max_tips_root_id_2_ = 0;
    ad_btn_id_1_ = 0;
    ad_btn_id_2_ = 0;

    ad_init_rc_1_ = nil;
    ad_init_rc_2_ = nil;

    current_show_ad_index_ = 0;

    is_ad_show_ = "false";
};

function social_push_tips_view.on_tips_root_create(uilib, ctrl_id)
    local out = comm_center.call_service("i_service_mainframe_get_current_mode", {});
    if out.mode == 1 then --min
        if social_push_tips_view.max_tips_root_id_1_ == 0 then
            social_push_tips_view.max_tips_root_id_1_ = ctrl_id;
            social_push_tips_view.ad_btn_id_1_ = ui_utility.get_ctrl_id_by_path(ctrl_id, "AdBtn");

            local ptr = ui_utility.get_ctrl_ptr_by_id(social_push_tips_view.ad_btn_id_1_);
            social_push_tips_view.ad_init_rc_1_ = ptr:GetRectEx(FALSE,TRUE);
        else
            social_push_tips_view.max_tips_root_id_2_ = ctrl_id;
            social_push_tips_view.ad_btn_id_2_ = ui_utility.get_ctrl_id_by_path(ctrl_id, "AdBtn");
            
            local ptr = ui_utility.get_ctrl_ptr_by_id(social_push_tips_view.ad_btn_id_2_);
            social_push_tips_view.ad_init_rc_2_ = ptr:GetRectEx(FALSE,TRUE);
        end

        if social_push_tips_mgr.is_im_inited_ == true then
            social_push_tips_view.reset_ad();
        end
    end
end

function social_push_tips_view.reset_ad()
    if social_push_tips_view.max_tips_root_id_1_ == 0 or social_push_tips_view.max_tips_root_id_2_ == 0 then
        return;
    end

    social_push_tips_view.current_show_ad_index_ = 1;
    social_push_tips_view.stop_show_timer();
    social_push_tips_view.stop_animation_timer();

    if #social_push_tips_mgr.ad_list_ > 0 then
        social_push_tips_view.show_ad_area('true');
        social_push_tips_view.update_ad();
    else
        social_push_tips_view.show_ad_area('false');
    end

end

function social_push_tips_view.update_ad()
    if social_push_tips_view.ad_btn_id_1_ == 0 or social_push_tips_view.ad_btn_id_2_ == 0 then
        return;
    end
    --选择一条广告进行显示
    local cur_ad = social_push_tips_view.get_current_show_ad();
    if cur_ad == nil then
        return;
    end

    ui_utility.setproperty_by_id(social_push_tips_view.ad_btn_id_1_, "Caption", cur_ad.title);
    ui_utility.setproperty_by_id(social_push_tips_view.ad_btn_id_2_, "Caption", cur_ad.title);

    -- 复位tips显示位置
    local ptr = ui_utility.get_ctrl_ptr_by_id(social_push_tips_view.ad_btn_id_1_);
    ptr:MoveEx(social_push_tips_view.ad_init_rc_1_,TRUE);

    local ptr = ui_utility.get_ctrl_ptr_by_id(social_push_tips_view.ad_btn_id_2_);
    ptr:MoveEx(social_push_tips_view.ad_init_rc_2_,TRUE);

    --启动显示时间定时器
    if social_push_tips_view.get_total_ad_cnt() > 1 then
        social_push_tips_view.start_show_timer();
    end

    --递增
    social_push_tips_view.current_show_ad_index_ = social_push_tips_view.current_show_ad_index_ + 1;
end

function social_push_tips_view.on_click_close_btn(uilib, ctrl_id)
    local item = social_push_tips_view.get_current_show_ad();
    if item ~= nil then
        social_push_tips_mgr.delete_one_item(item.id);
    end
    social_push_tips_view.show_ad_area('false');

    social_push_tips_mgr.clear_all_item();
    social_title_msg_tips_mgr.update_tips_msg_info({});

    social_push_tips_view.stop_show_timer();
    social_push_tips_view.stop_animation_timer();


    comm_center.call_service('i_service_increase_mini_mode_social_ad_colse_cnt', {});
end

function social_push_tips_view.on_click_ad(uilib, ctrl_id)
    local cur_ad = social_push_tips_view.get_current_show_ad();
    if cur_ad ~= nil then
        web_utility.open_new_browser(cur_ad.url, true, false, true, cur_ad.title, 1080, 700, 0);
    end

    comm_center.call_service('i_service_increase_mini_mode_social_ad_click_cnt', {});
end

function social_push_tips_view.get_current_show_ad()
    local total_cnt = #social_push_tips_mgr.ad_list_;
    if total_cnt == 0 then
        return nil;
    end
    local realy_index = social_push_tips_view.current_show_ad_index_ % total_cnt
    if realy_index == 0 then
        realy_index = total_cnt;
    end
    return social_push_tips_mgr.ad_list_[realy_index];
end

function social_push_tips_view.get_total_ad_cnt()
    return #social_push_tips_mgr.ad_list_;
end

function social_push_tips_view.is_ad_show()
    return social_push_tips_view.is_ad_show_;
end

function social_push_tips_view.show_ad_area(bShow)
    if social_push_tips_view.max_tips_root_id_1_ > 0 and social_push_tips_view.max_tips_root_id_2_ > 0 then
        social_push_tips_view.is_ad_show_ = bShow;

        local pid_1 = ui_utility.get_ctrl_parent_id(social_push_tips_view.max_tips_root_id_1_);
        local ptr_1 = ui_utility.get_ctrl_ptr_by_id(pid_1);

        local pid_2 = ui_utility.get_ctrl_parent_id(social_push_tips_view.max_tips_root_id_2_);
        local ptr_2 = ui_utility.get_ctrl_ptr_by_id(pid_2);
 
        if pid_1 > 0 then
            ui_utility.setproperty_by_id(pid_1, 'Visible', bShow);
        end
        if pid_2 > 0 then
            ui_utility.setproperty_by_id(pid_2, 'Visible', bShow);
        end
        if bShow == 'true' then
            event_center.send_event('social_event.SOCIAL_TIPS_SHOW', base_utility.table_to_json({is_show = true, height = 28}));
        else
            event_center.send_event('social_event.SOCIAL_TIPS_SHOW', base_utility.table_to_json({is_show = false, height = 28}));
        end
    end
end

--显示时间定时器
function social_push_tips_view.start_show_timer()
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:AddInterval('social_push_tips_view.on_process_show_timer', 60 * 1000);
end

function social_push_tips_view.stop_show_timer()    
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:RemoveInterval('social_push_tips_view.on_process_show_timer');
end

function social_push_tips_view.on_process_show_timer()
    social_push_tips_view.stop_show_timer();
    --启动滚动动画定时器
    social_push_tips_view.start_animation_timer();
end

--翻滚动画定时器
function social_push_tips_view.start_animation_timer()
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:AddInterval('social_push_tips_view.on_process_animation_timer', 80);
end

function social_push_tips_view.stop_animation_timer()    
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:RemoveInterval('social_push_tips_view.on_process_animation_timer');
end

function social_push_tips_view.on_process_animation_timer()
    local ptr1 = ui_utility.get_ctrl_ptr_by_id(social_push_tips_view.ad_btn_id_1_);
    local rc = ptr1:GetRectEx(FALSE,TRUE);
    local h = rc.bottom - rc.top
    rc.top = rc.top - 2;
    rc.bottom = rc.top + h;
    ptr1:MoveEx(rc,TRUE);

    local ptr2 = ui_utility.get_ctrl_ptr_by_id(social_push_tips_view.ad_btn_id_2_);
    local rc = ptr2:GetRectEx(FALSE,TRUE);
    local h = rc.bottom - rc.top
    rc.top = rc.top - 2;
    rc.bottom = rc.top + h;
    ptr2:MoveEx(rc,TRUE);

    if rc.bottom <= 0 then
        social_push_tips_view.stop_animation_timer();
        social_push_tips_view.update_ad();
    end
end

social_push_tips_mgr.init();